<?php
class m_login extends CI_Model {

    
    function __construct() {
        parent::__construct();
    }
    function db_login($loginid, $password)
    {

        $sql = "CALL PortalLogin('".$loginid."','".$password."')";
        $query = $this->db->query($sql);
        $result = array('result'=>false,
                'message'=>'system error, please try again later',
                'advertiser_id'=>0);
        
        if ($query->num_rows() > 0)
        {
           $row = $query->row();
           
           $result['result'] = ($row->advertiser_id == 0) ? false : true;
           $result['message']=$row->retResult;
           $result['advertiser_id'] = $row->advertiser_id;
           
        } 
        return $result;
                            
    }

    
    function register_user($data)
    {

        //$sha1_password =sha1($password);
        $query_str = "INSERT INTO advertisers(name,loginid,password,email,contact_person,phone_comp,phone_mobile,fax,activation_code) VALUES(?,?,?,?,?,?,?,?,?)";

        $this->db->query($query_str,$data);
    }

    function check_exists_loginid($loginid)
    {
        $query_str = "SELECT loginid from advertisers where loginid = ?";
        $result = $this->db->query($query_str,$loginid);

        if ($result->num_rows() > 0)
            return true;
        else
            return false;
    }
            
    function activate_account($activation_code)
    {
        $sql = "CALL ActivateRegistration('". $activation_code."')";
        $query = $this->db->query($sql);
        $return = array('result'=>false,
                'message'=>'system error, please try again later',
                'advertiser_id'=>0);
        
        if ($query->num_rows() > 0)
        {
           $row = $query->row();
           
           $return['result'] = ($row->advertiser_id == 0) ? false : true;
           $return['message']=$row->retResult;
           $return['advertiser_id'] = $row->advertiser_id;           
        } 
        return $return;
    }

    function account_login($loginid, $password)
    {
                                                                                                                                                    
        $query = $this->db->get_where('advertisers',array('loginid'=>$loginid)) ;        
        if ($query->num_rows() > 0) {

            //get the result as an array
            $query = $query->row_array();

            // retrieve the userid, username , password, account status
            $account_locked = $query['locked_status'];

            // check the account status  
            if ($account_locked == 1)
                return 'locked';

            // fetch the advertiser_id, username and password
            $advertiser_id = $query['id'];
            
            if (!$this->session->userdata('advertiser_id')) {
                $this->session->set_userdata('advertiser_id', $advertiser_id);
            } 
                                                                                
            // if passwords do not match insert a new record in the db sessions.
            if (md5($password) != $query['password']) {
                $session_id = $this->session->userdata('session_id');
                return ($this->update_ci_session($session_id, $advertiser_id));
            } 
            else 
            {
                $this->update_users_activity($advertiser_id,1);
                //$this->update_session_start($advertiser_id,$session_id);
                return 'ok:'.$advertiser_id;
            }
        } else {
            return 'wrong_id';
        }        
    }
    
    function update_ci_session($session_id, $advertiser_id) 
    {
        if (isset( $this->session->userdata['login_attempt']))
            $attempt = $this->session->userdata['login_attempt'];
        else
            $attempt =0;
        if ($attempt == 0) 
        {
            // update the current session login attempt
            $this->session->set_userdata('login_attempt',1);
            return 'wrong_password';

        } 
        else if ($attempt > 0) 
        {
            $attempt = $this->session->userdata['login_attempt'] + 1;
            $this->session->set_userdata('login_attempt',$attempt);
        }

        // check the number of attempts , if 3 return account locked, if not return false, 3 is the max attempts
        if ($attempt === 3) 
        {
            $this->lock_user_account($advertiser_id);
            return 'locked';
        } else  
            return 'wrong_password';
    }

                              
    function lock_user_account($advertiser_id) 
    {
        // update the users account status to locked === yes
        $this->db->where('id', $advertiser_id);
        $this->db->update('advertisers', array('locked_status' => 1));
        $this->session->sess_destroy();

    }

    function update_users_activity($advertiser_id,$loggedin) {
        $this->db->where('id', $advertiser_id);
        $this->db->update('advertisers', array('logged_in' => $loggedin));
   
        if ($loggedin==1)
        { 
             $session_id= $this->session->userdata('session_id');        
             $this->update_session_start($advertiser_id,$session_id);
        }
    }

    function update_session_start($advertiser_id, $session_id) {

        $session_start = time();
        $this->db->where('session_id', $session_id);
        $this->db->update('inet_sessions', array('session_start' => $session_start, 'advertiser_id' => $advertiser_id));
    }

    function update_session_end($advertiser_id, $session_id) {

        $this->update_users_activity ($advertiser_id,0);
        $session_end = time();
        $this->db->where('session_id', $session_id);
        $this->db->update('inet_sessions', array('session_end' => $session_end, 'advertiser_id' => $advertiser_id));
    }
     
  
} // End Class m_main

?>
